package com.yaozheng.vocationaltraining.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.dialog.interfaces.BaseDialogEventProcess;

public abstract class BaseDialog extends Dialog implements
        BaseDialogEventProcess, DialogInterface.OnKeyListener {
    protected boolean isCover;
    protected int gravity;
    /**
     * 返回隐藏 true 隐藏 false 不隐藏 默认true
     */
    protected boolean backHide;

    protected BaseActivity baseActivity;

    public BaseDialog(Context context, int gravity) {
        this(context, R.style.baseDialogStyleTheme, true, gravity, true);
    }

    public BaseDialog(Context context, int theme, boolean isCover, int gravity) {
        this(context, theme, isCover, gravity, true);
    }

    public BaseDialog(Context context, int theme, boolean isCover, int gravity, boolean backHide) {
        super(context, theme);
        if (context instanceof BaseActivity) {
            this.baseActivity = (BaseActivity) context;
        }
        this.isCover = isCover;
        this.gravity = gravity;
        this.backHide = backHide;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        initBaseDialog();
    }

    /**
     * 设置内容
     */
    public abstract void setContentView();

    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (!backHide) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 初始化BaseDialog
     */
    public void initBaseDialog() {
        Window window = getWindow();
        WindowManager.LayoutParams layoutpara = window
                .getAttributes();
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        layoutpara.gravity = gravity;
        if (isCover) {
            layoutpara.dimAmount = (float) (getContext().getResources().getInteger(R.integer.baseDialogdimAmount) / 10.0); // 添加背景遮盖
        } else {
            layoutpara.dimAmount = 0f;
        }

        layoutpara.width = baseActivity.getWindow().getAttributes().width;
        setAttributes(window, layoutpara);
        setCanceledOnTouchOutside(backHide);
        setOnKeyListener(this);
    }

    /**
     * 设置属性
     */
    protected void setAttributes(Window window,
                                 WindowManager.LayoutParams layoutpara) {
    }

    @Override
    public void runMethod(String methodName) {
        try {
            if (methodName != null && !"".equals(methodName)) {
                getSubClasses().getMethod(String.valueOf(methodName), new Class[0]).invoke(
                        getSubObject(), new Object[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            error(e);
        }
    }

    public void error(Exception e) {
    }

    /**
     * 得到子类class
     *
     * @return
     */
    public Class getSubClasses() {
        return getClass();
    }

    /**
     * 得到子类对象
     *
     * @return
     */
    public Object getSubObject() {
        return this;
    }

    public void baseCancel() {
        cancel();
    }
}
